package cn.ac.big.bigd.util;

import java.io.*;

/**
 * Created by wangfan on 2017/7/14.
 */
public class FileUtil {

    public static void deleteAllFilesOfDir(File path) {
        if (!path.exists())
            return;
        if (path.isFile()) {
            path.delete();
            return;
        }
        File[] files = path.listFiles();
        for (int i = 0; i < files.length; i++) {
            deleteAllFilesOfDir(files[i]);
        }
        path.delete();
    }

    public static String  getFileName(String path){
        path = path.substring(path.lastIndexOf("/")+1);
        path = path.substring(path.lastIndexOf("\\")+1);
        return path;
    }


    public static int getFileLineCount(String filename) {
        int cnt = 0;
        LineNumberReader reader = null;
        try {
            reader = new LineNumberReader(new FileReader(filename));
            @SuppressWarnings("unused")
            String lineRead = "";
            while ((lineRead = reader.readLine()) != null) {
            }
            cnt = reader.getLineNumber();
        } catch (Exception ex) {
            cnt = -1;
            ex.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return cnt;
    }
    public static boolean saveFile(byte b[], String filePath) {
        try {
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
            out.write(b);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(FileUtil.getFileName("D:\\IdeaProjects"));
    }
}
